---
title: Spatial Queries on a Feature Layer
description: Create detailed spatial queries like point within polygon or line intersects polygons with Feature Layer queries. Some combinations will not result in any features being highlighted.
layout: example.hbs
---

<style>
  #panel {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 1000;
    background: white;
    padding: 10px;
  }
</style>

<div id="map"></div>

<div id="panel" class="leaflet-bar">
  Neighborhoods
  <select name="relation" id="relationSelect">
    <option value="within">Within<options>
    <option value="contains">Contains<options>
    <option value="intersects">Intersects<options>
    <option value="overlaps">Overlaps<options>
  </select>
  <select name="geometry" id="geometrySelect">
    <option value="bounds">Bounds<options>
    <option value="point">Point<options>
    <option value="line">Line<options>
    <option value="polygon">Polygon<options>
  </select>
</div>
<script>
  var map = L.map("map").setView([45.525, -122.628], 11);

  L.esri.basemapLayer("Topographic").addTo(map);

  // create our layer
  neighborhoods = L.esri.featureLayer({
    url: "https://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/Neighborhoods_pdx/FeatureServer/0",
    style: {
      color: 'gray',
      weight: 1,
      opacity: 0.4
    }
  }).addTo(map);

  // create a bounds object to query against
  var bounds = L.latLngBounds([
    [45.494556, -122.691536],
    [45.538100, -122.608452]
  ]);

  // create a marker object to query against
  var marker = L.marker([45.571034, -122.686386]).addTo(map);

  // create a rectangle to visualize the bounds
  var rect = L.rectangle(bounds, {
    color: 'blue',
    weight: 2
  }).addTo(map);

  // create a line to query against
  var line = L.polyline([
    [45.559256, -122.611885],
    [45.502256, -122.562790],
    [45.483244, -122.620468]
  ], {
    color: 'blue',
    weight: 2
  }).addTo(map);

  // create a polygon to query against
  var polygon = L.polygon([
    [45.489261, -122.737198],
    [45.486373, -122.688446],
    [45.462539, -122.682266],
    [45.441104, -122.693596],
    [45.444717, -122.722435],
    [45.461094, -122.738228],
    [45.482521, -122.734108],
    [45.489261, -122.737198]
  ], {
    color: 'blue',
    weight: 2
  }).addTo(map);

  // collect geometries into an object so we can reference them later
  var geometries = {
    bounds: bounds,
    line: line,
    polygon: polygon,
    point: marker
  };

  // get references to our <select> elements
  var relationship = document.getElementById('relationSelect');
  var geometry = document.getElementById('geometrySelect');

  var previousIds = [];

  // reset all features back to their regularly defined styles
  function reset(){
    for (var i = previousIds.length - 1; i >= 0; i--) {
      neighborhoods.resetStyle(previousIds[i]);
    };
  }

  // query the API and highlight features
  function query(){
    reset();

    // lookup our input geometry
    var inputGeometry = geometries[geometry.value];

    // query the service executing the selected relation with the selected input geometry
    neighborhoods.query()[relationship.value](inputGeometry).ids(function(error, ids){
      previousIds = ids;
      for (var i = ids.length - 1; i >= 0; i--) {
        neighborhoods.setFeatureStyle(ids[i], { color: 'red', weight: 2 });
      };
    });
  }

  // query when an input changes
  geometry.addEventListener('change', query);
  relationship.addEventListener('change', query);

  // once all neighborhoods have loaded run the default query
  neighborhoods.once('load', function(){
    query();
  });

</script>
